/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：AdvCfgService.java
 * History:
 *         2012-6-28: Initially created, Chrise.
 */
package com.xteam.ecsystem.service.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.AdvPosition;
import com.xteam.ecsystem.base.AdvType;
import com.xteam.ecsystem.base.CommendType;
import com.xteam.ecsystem.base.NormalPage;
import com.xteam.ecsystem.base.NormalState;
import com.xteam.ecsystem.base.SystemContent;
import com.xteam.ecsystem.dao.IAdvCfgDao;
import com.xteam.ecsystem.dao.IArticlesDao;
import com.xteam.ecsystem.dao.IArticlesSortDao;
import com.xteam.ecsystem.dao.IGoodsDao;
import com.xteam.ecsystem.dao.IGoodsGroupDao;
import com.xteam.ecsystem.entities.SysAdvConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 广告配置服务层。
 * @author Chrise
 */
@Service
public class AdvCfgService {
	
	@Autowired
	private IAdvCfgDao acDao;
	
	@Autowired
	private IGoodsGroupDao ggDao;
	
	@Autowired
	private IGoodsDao giDao;
	
	@Autowired
	private IArticlesSortDao agDao;
	
	@Autowired
	private IArticlesDao aiDao;
	
	/**
	 * 获取广告配置。
	 * @author Chrise 2012-6-28
	 * @param sacType
	 * @param sacName
	 * @param sacPage
	 * @param sacPosition
	 * @param sacState
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageCenterStation<SysAdvConfig> getAdvConfigs(int sacType, String sacName, 
		int sacPage, int sacPosition, int sacState, int pageIndex, int pageSize) {
		if (sacType == AdvType.ALL.value()) sacType = IAdvCfgDao.INVALID_TYPE;
		if (sacName == null || "".equals(sacName)) sacName = IAdvCfgDao.INVALID_NAME;
		if (sacPage == NormalPage.ALL.value()) sacPage = IAdvCfgDao.INVALID_PAGE;
		if (sacPosition == AdvPosition.ALL.value()) sacPosition = IAdvCfgDao.INVALID_POSITION;
		if (sacState == NormalState.ALL.value()) sacState = IAdvCfgDao.INVALID_STATE;
		
		return this.acDao.getConfigs(sacType, sacName, sacPage, sacPosition, sacState, pageIndex * pageSize, pageSize);
	}
	
	/**
	 * 切换状态。
	 * @author Chrise 2012-6-28
	 * @param sacId
	 * @param sacTo
	 */
	public void switchState(String sacId, boolean sacTo) {
		if (sacTo) {
			this.acDao.updateState(sacId, NormalState.EFFECTIVE.value());
		} else {
			this.acDao.updateState(sacId, NormalState.INVALID.value());
		}
	}
	
	/**
	 * 删除广告配置。
	 * @author Chrise 2012-6-28
	 * @param ids
	 */
	public void delAdvConfigs(List<String> ids) {
		this.acDao.deleteConfigs(ids);
	}
	
	/**
	 * 保存广告配置。
	 * @author Chrise 2012-6-28
	 * @param sac
	 * @return
	 */
	public boolean saveAdvConfig(SysAdvConfig sac) {
		if (sac.getSacState() == null) {
			sac.setSacState(NormalState.EFFECTIVE.value());
		}
		
		if (sac.getSacId() == null || "".equals(sac.getSacId())) {
			if (this.acDao.isExist(sac.getSacName())) return false;
			
			this.acDao.insertConfig(sac);
		} else {
			if (this.acDao.isExist(sac.getSacId(), sac.getSacName())) return false;
			
			this.acDao.updateConfig(sac);
		}
		
		return true;
	}
	
	/**
	 * 获取广告配置。
	 * @author Chrise 2012-6-28
	 * @param id
	 * @return
	 */
	public SysAdvConfig getAdvConfig(String id) {
		return this.acDao.getConfig(id);
	}
	
	/**
	 * 获取广告内容。
	 * @author Chrise 2012-6-28
	 * @param type
	 * @return
	 */
	public List<SystemContent> getAdvContent(int type) {
		List<Map<String, String>> scl = null;
		
		AdvType at = AdvType.valueOf(type);
		switch (at) {
			case COMMEND:
				scl = CommendType.getCommendType();
				break;
			case GDSGROUP:
				scl = this.ggDao.getSystemContent(NormalState.EFFECTIVE.value(), 
					SystemContent.FN_VALUE, SystemContent.FN_TEXT, SystemContent.FN_PARENT);
				break;
			case GDSCONTENT:
				scl = this.giDao.getSystemContent(NormalState.EFFECTIVE.value(), 
					SystemContent.FN_VALUE, SystemContent.FN_TEXT, SystemContent.FN_PARENT);
				break;
			case ARTGROUP:
				scl = this.agDao.getSystemContent(NormalState.EFFECTIVE.value(), 
					SystemContent.FN_VALUE, SystemContent.FN_TEXT, SystemContent.FN_PARENT);
				break;
			case ARTCONTENT:
				scl = this.aiDao.getSystemContent(NormalState.EFFECTIVE.value(), 
					SystemContent.FN_VALUE, SystemContent.FN_TEXT, SystemContent.FN_PARENT);
				break;
		}
		
		List<SystemContent> list = new ArrayList<SystemContent>();
		
		if (scl != null) {
			for (Map<String, String> sc : scl) {
				list.add(new SystemContent(sc.get(SystemContent.FN_VALUE), 
					sc.get(SystemContent.FN_TEXT), sc.get(SystemContent.FN_PARENT)));
			}
		}
		
		return list;
	}
	
	/**
	 * 获取广告内容。
	 * @author Chrise 2012-6-29
	 * @param type
	 * @param name
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageCenterStation<SystemContent> getAdvContents(int type, String name, int pageIndex, int pageSize) {
		AdvType at = AdvType.valueOf(type);
		switch (at) {
			case GDSGROUP:
				return this.ggDao.getSystemContents(name, NormalState.EFFECTIVE.value(), pageIndex * pageSize, pageSize);
			case GDSCONTENT:
				return this.giDao.getSystemContents(name, NormalState.EFFECTIVE.value(), pageIndex * pageSize, pageSize);
			case ARTGROUP:
				return this.agDao.getSystemContents(name, NormalState.EFFECTIVE.value(), pageIndex * pageSize, pageSize);
			case ARTCONTENT:
				return this.aiDao.getSystemContents(name, NormalState.EFFECTIVE.value(), pageIndex * pageSize, pageSize);
		}
		
		return new PageCenterStation<SystemContent>(0, null);
	}
}
